package 力扣日常刷题.木22二月.第13天219;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author 帅小伙
 * @date 2022/2/19
 * @description
 */
public class Demo98数组中第k大的数 {

    public int findKthLargest(int[] nums, int k) {
        int len = nums.length;
        PriorityQueue<Integer> heap = new PriorityQueue<>(k, (a,b)->(a-b));

        for (int i = 0; i < k; i++) {
            heap.offer(nums[i]);
        }

        for (int i = k; i < len; i++) {
            Integer poll = heap.peek();
            if(nums[i] >poll){
                heap.poll();
                heap.offer(nums[i]);
            }
        }
        return heap.peek();
    }

}
